package com.snails.ui.toast;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.DrawableRes;
import androidx.annotation.StringRes;

import com.snails.ui.R;

/**
 * Created by lawrence on 2018/11/21.
 * <p>
 * 带有图片的Toast，图片在上，文字在下
 */

public class CToast extends Toast {
    // =====================================================================================
    // ---- 成员变量 ----
    // ==========================================
    private Context mCtx;
    private Builder mBuilder = null;

    // =====================================================================================
    // ---- 重载基类方法 ----
    // ==========================================
    private CToast(Context context, Builder builder) {
        super(context);
        this.mCtx = context;
        makeImageText(mBuilder = builder);
    }

    /**
     * 构造含有图片文本的Toast
     */
    protected void makeImageText(Builder builder) {
        LayoutInflater inflate = (LayoutInflater) mCtx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflate.inflate(R.layout.toast_image_text_notification, null);
        setUI(layout, builder).setView(layout);
    }

    private CToast setUI(View layout, Builder builder) {
        ImageView iv = (ImageView) layout.findViewById(R.id.toast_image);
        View v = layout.findViewById(R.id.toast_space);
        TextView tv = (TextView) layout.findViewById(R.id.toast_message);
        setImageView(builder, iv).setTextView(builder, tv)
                .setSpace(builder, v).setDuration(builder.mDuration);
        return this;
    }

    /**
     * 设置图片
     */
    private CToast setImageView(Builder builder, ImageView iv) {
        if (builder.mImageResId != -1) {
            iv.setVisibility(View.VISIBLE);
            iv.setImageResource(builder.mImageResId);
        } else {
            iv.setVisibility(View.GONE);
        }
        return this;
    }

    /**
     * 设置文本
     */
    private CToast setTextView(Builder builder, TextView tv) {
        String text = builder.mMsg;
        if (builder.mMsgResId != -1) {
            text = mCtx.getString(builder.mMsgResId);
        }
        if (TextUtils.isEmpty(text)) {
            tv.setVisibility(View.GONE);
        } else {
            tv.setVisibility(View.VISIBLE);
            tv.setText(text);
        }
        return this;
    }

    private CToast setSpace(Builder builder, View v) {
        String text = builder.mMsg;
        if (builder.mMsgResId != -1) {
            text = mCtx.getString(builder.mMsgResId);
        }
        v.setVisibility(builder.mImageResId != -1 && !TextUtils.isEmpty(text) ? View.VISIBLE : View.GONE);
        return this;
    }

    // =====================================================================================
    // ---- 对外方法 ----
    // ==========================================

    /**
     * 得到builder对象
     */
    public Builder getBuilder() {
        return mBuilder;
    }

    /**
     * 设置新的builder更改显示
     */
    public void setBuilder(Builder builder) {
        final View layout = getView();
        if (layout == null) {
            throw new RuntimeException("This CToast was not created with new CToast.Builder().build()");
        }
        setUI(layout, mBuilder = builder);
    }

    // =====================================================================================
    // ---- builder构造器 ----
    // ==========================================

    /**
     * 设置Toast参数
     */
    public static class Builder {
        private String mMsg = "";
        private int mDuration = Toast.LENGTH_SHORT;
        @DrawableRes private int mImageResId = -1;
        @StringRes private int mMsgResId = -1;

        /**
         * 设置显示文本
         */
        public Builder message(String message) {
            this.mMsg = message;
            return this;
        }

        /**
         * 设置显示文本，设置此参数，会覆盖message(String message)设置
         */
        public Builder message(@StringRes int messageId) {
            this.mMsgResId = messageId;
            return this;
        }

        /**
         * 设置显示时间，建议使用Toast.LENGTH_SHORT或者Toast.LENGTH_LONG
         */
        public Builder duration(int duration) {
            this.mDuration = duration;
            return this;
        }

        /**
         * 设置显示的本地图片资源
         */
        public Builder image(@DrawableRes int drawableRes) {
            this.mImageResId = drawableRes;
            return this;
        }

        /**
         * 构造CToast
         *
         * @param context activity或者application
         */
        public CToast build(Context context) {
            return new CToast(context, this);
        }

    }

}
